package com.yqy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqy.entity.MessageBoard;
import com.yqy.mapper.MessageBoardMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author yiqianyi
 * @version 1.0
 * @date 2022-03-20 14:28
 */
@CrossOrigin
@RestController
@Api(tags = {"留言板控制层"})
public class MessageBoardController {
    @Autowired
    public MessageBoardMapper messageBoardMapper;

    @PostMapping("/message/getPage")
    @ApiOperation(value = "分页获取留言板信息", notes = "/message/getPage")
    public IPage<MessageBoard> getPageStudentInfo(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "messageBoard", value = "messageBoard", required = true) MessageBoard messageBoard) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!("").equals(messageBoard.getMessage())) {
            queryWrapper.like("message", messageBoard.getMessage());
        }
        if (!("").equals(messageBoard.getPerson())) {
            queryWrapper.eq("person", messageBoard.getPerson());
        }
        Page<MessageBoard> studentInfoPage = new Page<>(pageNum, pageSize);
        return messageBoardMapper.selectPage(studentInfoPage, queryWrapper);
    }

    @PostMapping("/message/update")
    @ApiOperation(value = "修改留言消息",notes = "/message/update")
    public int update(@RequestBody @ApiParam(name = "messageBoard", value = "messageBoard", required = true) MessageBoard messageBoard){
        return messageBoardMapper.updateById(messageBoard);
    }

    @PostMapping("/message/insert")
    @ApiOperation(value = "新增留言消息",notes = "/message/insert")
    public int insert(@RequestBody @ApiParam(name = "messageBoard", value = "messageBoard", required = true) MessageBoard messageBoard){
        return messageBoardMapper.insert(messageBoard);
    }

    @PostMapping("/message/delete")
    @ApiOperation(value = "批量删除留言信息", notes = "/message/delete")
    public int delete (@RequestParam @ApiParam(name = "ids",value = "1,2,3",required = true) String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return messageBoardMapper.deleteBatchIds(list);
    }
}
